package admin_settings_api

import (
	"blog_go/config"
	"blog_go/core"
	"blog_go/global"
	"blog_go/models/res"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type SettingsUpdateReq struct {
	ConfName string `uri:"conf_name"` // 大写才行,不然ShouldBindUri赋值不了
}

// 修改site信息
func (AdminSettingsApi) SettingsSiteInfoUpdateApi(c *gin.Context) {
	info := config.SiteInfo{}
	err := c.ShouldBind(&info)
	if err != nil {
		res.FailWithCode(res.ParamError, c)
		return
	}
	global.Conf.SiteInfo = info
	logrus.Info(global.Conf.SiteInfo, "------", info)
	err = core.SetYaml()
	if err != nil {
		res.FailWithMessage("修改失败", c)
	}
	res.OkWithDate(true, c)
}

// 修改email
func (AdminSettingsApi) SettingsEmailUpdateApi(c *gin.Context) {
	info := config.Email{}
	err := c.ShouldBind(&info)
	if err != nil {
		res.FailWithCode(res.ParamError, c)
		return
	}
	global.Conf.Email = info
	logrus.Info(global.Conf.SiteInfo, "------", info)
	err = core.SetYaml()
	if err != nil {
		res.FailWithMessage("修改失败", c)
	}
	res.OkWithDate(true, c)
}

func (AdminSettingsApi) SettingsInfoUpdateApi(c *gin.Context) {
	var req SettingsUpdateReq
	err := c.ShouldBindUri(&req)
	if err != nil {
		logrus.Error(err)
		res.FailWithCode(res.ParamError, c)
		return
	}
	switch req.ConfName {
	case "site_info":
		var siteInfo config.SiteInfo
		err := c.ShouldBindJSON(&siteInfo)
		if err != nil {
			res.FailWithMessage("site_info信息修改失败", c)
			return
		}
		global.Conf.SiteInfo = siteInfo
	case "email":
		var email config.Email
		err := c.ShouldBindJSON(&email)
		if err != nil {
			res.FailWithMessage("email信息修改失败", c)
			return
		}
		global.Conf.Email = email
	case "jwt":
		var jwt config.Jwt
		err := c.ShouldBindJSON(&jwt)
		if err != nil {
			res.FailWithMessage("jwt信息修改失败", c)
			return
		}
		global.Conf.Jwt = jwt
	case "qi_niu":
		var qn config.Qiniu
		err := c.ShouldBindJSON(&qn)
		if err != nil {
			res.FailWithMessage("qi_niu信息修改失败", c)
			return
		}
		global.Conf.Qiniu = qn
	case "qq":
		var qq config.QQ
		err := c.ShouldBindJSON(&qq)
		if err != nil {
			res.FailWithMessage("qq信息修改失败", c)
			return
		}
		global.Conf.QQ = qq
	default:
		res.FailWithMessage("uri错误", c)
		return
	}
	err = core.SetYaml()
	if err != nil {
		res.FailWithMessage("信息修改失败", c)
	}
}
